package hsintiao.com.xintiaoAPP.controller;


import hsintiao.com.xintiaoAPP.annotation.PassToken;
import hsintiao.com.xintiaoAPP.entity.User;
import hsintiao.com.xintiaoAPP.entity.vo.UserYVO;
import hsintiao.com.xintiaoAPP.service.LoginService;
import hsintiao.com.xintiaoAPP.service.UserService;
import hsintiao.com.xintiaoAPP.util.AjaxResult;
import hsintiao.com.xintiaoAPP.util.ResponseCode;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/log")
@Api(value="登录和注册",tags={"登录注册"})
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserService userService;


    /*
    * 登录注册
    * */
    @PostMapping("/login")
    @PassToken
    @ResponseBody
    public AjaxResult login(@RequestBody Map<String,Object> params,HttpServletResponse response,HttpServletRequest request){
        AjaxResult ajaxResult = loginService.login(params,response);
        Map<String,Object> resultObj = (Map<String, Object>) ajaxResult.getResultObj();
        if(resultObj != null){
            String token = (String) resultObj.get("token");
            System.out.println("登录token="+token);
            Cookie cookie = new Cookie("cookie", token);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24);
            //cookie.setDomain("192.168.31.41");
            response.addCookie(cookie);
        }
        String cookie = request.getHeader("cookie");
        System.out.println(cookie);
        return ajaxResult;
    }


    @PostMapping("/registerForY")
    @ResponseBody
    @PassToken
    public AjaxResult registerForYLY(@RequestBody UserYVO user){
        try {
            System.out.println(user);
            AjaxResult result = loginService.registerForYLY(user);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setCode(201).setMessage("注册失败");
        }
    }


    //发送短信，将短信内容存入redis
    @PassToken
    @PostMapping("/getSmsCode")
    @ResponseBody
    public AjaxResult getSmsCode(@RequestBody User user, HttpServletResponse response, HttpServletRequest request){
        String phone = user.getPhone();
        System.out.println("手机号为："+phone);
        try {
            AjaxResult ajaxResult = loginService.sendSMG(phone);
            Cookie cookie = new Cookie("cookie", "value");
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24);
            response.addCookie(cookie);
            return ajaxResult;
        }catch (Exception e){
            return AjaxResult.me().setCode(ResponseCode.FAILURE.getCode())
                    .setSuccess(false).setMessage("error:"+"芯跳" +
                            "");
        }

    }

}
